//
// Created by antler on 2018/12/11.
//

#include "StringUtils.h"

namespace StringUtils {

    int stoi(const std::string &s, int def_value) {
        int d = def_value;
        try {
            d = std::stoi(s, 0, 0);
        }
        catch (...) {
        }
        return d;
    }

    void split(const std::string &s, const std::string &sep, std::vector<std::string> &vec) {
        int off = 0;
        do {
            int pos = s.find(sep, off);
            if (pos == std::string::npos) {
                vec.push_back(s.substr(off));
                break;
            }
            vec.push_back(s.substr(off, pos - off));
            off = pos + 1;
        } while (1);
    }

    bool startWith(const std::string &s, const std::string &prefix) {
        return (0 == s.find(prefix));
    }

    bool endWith(const std::string &s, const std::string &suffix) {
        std::string::size_type pos = s.rfind(suffix);
        if (pos == std::string::npos)
            return false;
        return (pos == (s.length() - suffix.length()));
    }

    std::string removeExtension(const std::string &s) {
        size_t pos = s.find_last_of('.');
        return s.substr(0, pos);
    }
};